package com.softer.chengxin.param.service.impl;

import com.softer.chengxin.common.config.CourseException;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.LevelEnum;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.enums.SignEnum;
import com.softer.chengxin.param.dao.GradeDao;
import com.softer.chengxin.param.entity.Grade;
import com.softer.chengxin.param.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Gong Zhiyang
 * @date 2019/12/14
 */
@Service
public class GradeServiceImpl implements GradeService {
    @Autowired
    private GradeDao gradeDao;

    /**
     * 保存评级设置，根据主键，有则修改，无则增加
     *
     * @param list 评级集合
     * @return 保存结果
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public CommonResult save(List<Grade> list, String loginName) {
        //取出 没有 负面信息的 集合
        List<Grade> noBadList = list.stream().filter(o -> o.getBadInfo() == 0).collect(Collectors.toList());
        checkOverlap(noBadList);
        //取出 有 负面信息 的集合
        List<Grade> badList = list.stream().filter(o -> o.getBadInfo() == 1).collect(Collectors.toList());
        checkOverlap(badList);
        for (Grade grade : list) {
            grade.setCreater(loginName);
            grade.setModifier(loginName);
            int num = gradeDao.save(grade);
            if (num < 1) {
                throw new CourseException(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
            }
        }
        return CommonResult.response(NumberEnum.ONE.getNum());
    }

    /**
     * 区间校验
     *
     * @param list 校验集合
     */
    private static void checkOverlap(List<Grade> list) {
        list.stream().sorted(Comparator.comparing(Grade::getBiggerScore))
                .reduce((a, b) -> {
                    if (a.getBiggerScore().compareTo(b.getSmallerScore()) > 0) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "数字区间存在重叠区域");
                    } else if (a.getBiggerScore().compareTo(b.getSmallerScore()) < 0) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "数字区间未闭合");
                    }
                    if (SignEnum.LESS.getSign().equals(a.getBiggerSign()) && SignEnum.GREATER.getSign().equals(b.getSmallerSign())) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "符号区间未闭合");
                    } else if (SignEnum.LESS_OR_EQUAL.getSign().equals(a.getBiggerSign()) && SignEnum.GREATER_OR_EQUAL.getSign().equals(b.getSmallerSign())) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "符号区间存在重叠区域");
                    }
                    return b;
                });
    }

    /**
     * 获取评级列表
     *
     * @return 列表
     */
    @Override
    public List<Grade> getList() {
        return gradeDao.getList();
    }

    /**
     * 删除评级
     *
     * @param id 评级ID
     * @return 删除结果
     */
    @Override
    public CommonResult delete(int id) {
        int num = gradeDao.delete(id);
        return CommonResult.response(num);
    }

    /**
     * 查询信用等级列表
     * 用于下拉列表
     *
     * @return 信用等级下拉列表
     */
    @Override
    public List<String> getLevelList() {
        List<String> list = gradeDao.getLevelList();
        if (!list.isEmpty()) {
            list.add(LevelEnum.C_SERIOUS.getLevel());
            list.add(Common.CANCEL);
        }
        return list;
    }
}
